package sd.czw.com.mmgapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.adapter.CategoryGoodsAdapter;
import sd.czw.com.mmgapp.mcallback.CatageoryGoodsCallback;
import sd.czw.com.mmgapp.model.categorybean.CategoryFragmenGrideViewtInfo;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/11.
 */
public class RzActivity extends AppCompatActivity {

    private String id;
    private int width;
    private ImageView image;
    private MaterialRefreshLayout refresh;
    private boolean isLoadMore = true;
    private int start = 0;
    public static final int limit = 20;
    private int page = 0;
    private List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> cat_goods;
    private List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> gdatas = new ArrayList<>();
    private RecyclerView rv;
    private String path;
    private CategoryGoodsAdapter categoryGoodsAdapter;
    private String titles;
    private RelativeLayout rl_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rz);
        width = getWindowManager().getDefaultDisplay().getWidth();

        id = getIntent().getStringExtra("goods_id");
        path = getIntent().getStringExtra("path");
        titles = getIntent().getStringExtra("titles");

        View titlesview = findViewById(R.id.titles);
        rl_back = (RelativeLayout) titlesview.findViewById(R.id.rl_back);//返回
        rl_back.setVisibility(View.VISIBLE);
        rl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        TextView textView = (TextView) titlesview.findViewById(R.id.tv_title);//标题
        textView.setText(titles);
        titlesview.findViewById(R.id.rl_right);//搜索

        getdatas(start);
        rv = (RecyclerView) findViewById(R.id.rv);
        GridLayoutManager manager = new GridLayoutManager(this,2);
        rv.setLayoutManager(manager);
        rv.setHasFixedSize(true);
        rv.setNestedScrollingEnabled(false);
        categoryGoodsAdapter = new CategoryGoodsAdapter(this,width,path,gdatas);
        rv.setAdapter(categoryGoodsAdapter);
        refreshandloadmore();
    }

    private void getdatas(int s) {
        OkHttpUtils.post().url(Contants.FL_GOODS)
                .addParams("time",Contants.Time)
                .addParams("sign",Contants.Sign)
                .addParams("act","cat_goods")
                .addParams("cat_id",id)
                .addParams("start", String.valueOf(s))
                .addParams("limit", String.valueOf(limit))
                .build().execute(new CatageoryGoodsCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(CategoryFragmenGrideViewtInfo response, int id) {
                if (response.getErrcode() == 1){
                    setdatas(response.getErrmsg());
                }
            }
        });
    }

    private void setdatas(CategoryFragmenGrideViewtInfo.ErrmsgBean errmsg) {
        cat_goods = errmsg.getCat_goods();
        gdatas.addAll(cat_goods);
        categoryGoodsAdapter.notifyDataSetChanged();
    }

    /**
     * 刷新、加载更多
     */
    private void refreshandloadmore() {
        refresh = (MaterialRefreshLayout) findViewById(R.id.refresh);
        refresh.setSunStyle(true);
        refresh.setIsOverLay(true);
        refresh.setWaveShow(false);
        refresh.setLoadMore(isLoadMore);
        refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        start = 0;
                        gdatas.clear();
                        getdatas(start);
                        categoryGoodsAdapter.notifyDataSetChanged();
                        refresh.finishRefresh();
                    }
                }, 1000);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isLoadMore = false;
                        page++;
                        start = 20 * page;
                        getdatas(start);
                        categoryGoodsAdapter.notifyDataSetChanged();
                        refresh.finishRefreshLoadMore();
                    }
                }, 500);
            }
        });
    }
}
